package com.plugin.flutter_bugly_plugin

import android.content.Context
import androidx.annotation.NonNull
import com.plugin.flutter_bugly_plugin.utils.CrashUtil
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar


/** FlutterBuglyPlugin */
class FlutterBuglyPlugin : FlutterPlugin, MethodCallHandler {

    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var channel: MethodChannel

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        context = flutterPluginBinding.applicationContext
        channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_bugly_plugin")
        channel.setMethodCallHandler(this)
    }

    private var context: Context? = null

    // This static function is optional and equivalent to onAttachedToEngine. It supports the old
    // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
    // plugin registration via this function while apps migrate to use the new Android APIs
    // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
    //
    // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
    // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
    // depending on the user's project. onAttachedToEngine or registerWith must both be defined
    // in the same class.
    companion object {

        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val channel = MethodChannel(registrar.messenger(), "flutter_bugly_plugin")
            channel.setMethodCallHandler(FlutterBuglyPlugin())
        }
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        when (call.method) {
            "initBugly" -> {
                val params = call.arguments as? Map<*, *> ?: return
                val appId = params["appId"].toString()
                CrashUtil.initBugly(context!!, appId)
                result.success(true)
            }
            "postCrash" -> {
                val params = call.arguments as? Map<*, *> ?: return
                val crashMessage = params["crashMessage"].toString()
                val crashStack = params["crashStack"].toString()
                CrashUtil.postCrash(crashMessage, crashStack)
                result.success("")
            }
            else -> {
                result.notImplemented()
            }
        }

    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
        context = null
    }


}
